package Source;

import DAO.MySQLAccountDAO;
import Types.AccountType;
import Views.ViewMain;
import java.util.ArrayList;
import java.util.Hashtable;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.*;
import javax.naming.directory.DirContext;

/**
 * The model for login user
 * @author Josh and Sam
 */
public class LoginUser {

    private String username;
    private String password; //.createConnection();
    private AccountType account;

    /**
     * Puts the logged in users data into a accessable location for all to use
     */
    public void addToOnline() {
        /*switch (8) {
        case 1: MainUI.setOnlineUser(new User());
        }*/
        ViewMain.setOnlineUser(account);

    }

    public void logFailedAttempt() {
        //throw new UnsupportedOperationException("Not yet implemented");
    }

    /**
     * Sets the password
     * @param password
     */
    public void setPassword(String password) {
        this.password = password;
    }

    /**
     * Sets the username
     * @param username
     */
    public void setUsername(String username) {
        this.username = username;
    }

    /**
     * gets the username
     * @return String
     */
    public String getUsername() {
        return username;
    }

    /**
     * Checks the database for the username and password, returns true if it is found and false if not
     * @return boolean
     */
    public boolean authenticate() {
       /*Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
        env.put(Context.PROVIDER_URL, "ldap://ldap.jcu.edu.au:389/dc=jcu,dc=edu,dc=au");
        env.put(Context.SECURITY_AUTHENTICATION, "simple");
        env.put(Context.SECURITY_PRINCIPAL, "uid=" + username + ",ou=Users,dc=jcu,dc=edu,dc=au");
        env.put(Context.SECURITY_CREDENTIALS, password);
            if (true) {
            }
        //DirContext ctx = new InitialDirContext(env);
        DirContext ctx;
        try {
            ctx = new InitialDirContext(env);
            System.out.println("something: " );
        } catch (NamingException e) {
            System.out.println("Error Message: " + e.getMessage());
        }*/

        FlightServerInteraction logIn = new FlightServerInteraction();
        ArrayList<String> request = new ArrayList<String>();
        ArrayList<String> reply = new ArrayList<String>();
        request.add(0,"log");
        request.add(1,username);
        request.add(2,password);
        reply = logIn.run(request);

        account = new AccountType();
        if (reply.get(0).equals("victory")){
            account.setAddress(reply.get(1));
            account.setFirstName(reply.get(2));
            account.setLastName(reply.get(3));
            account.setUsername(reply.get(4));
            account.setPassword(reply.get(5));
            account.setLevel(reply.get(6));
            return true;
        }
        return false;
        //account = databaseConnection.validAccount(username, password);
       // if (account != null) {
       //     return true;
       // }
       // return false;
    }
}
